package com.google.code.tretris.events;

import java.util.Arrays;

import android.os.Parcel;
import android.os.Parcelable;

import com.google.code.tretris.common.WKC;

public class UpdateBoardEvent implements Parcelable {
	private int[] blocks = new int[WKC.BOARD_WIDTH*WKC.BOARD_HEIGHT];
	private final int x;
	private final int y;
	private final int rotation;
	private final char tetromino;
	private final String clientID;

	public UpdateBoardEvent(final String clientID, final int x, final int y,
			final int rotation, final char tetromino, final int[] blocks) {
		super();
		this.x = x;
		this.y = y;
		this.rotation = rotation;
		this.tetromino = tetromino;
		this.clientID = clientID;
		this.blocks = blocks;
	}

	public int[] getBlocks() {
		return blocks;
	}

	public int getX() {
		return x;
	}

	public int getY() {
		return y;
	}

	public int getRotation() {
		return rotation;
	}

	public char getTetromino() {
		return tetromino;
	}

	public String getClientID() {
		return clientID;
	}
		
	@Override
	public String toString() {
		return "UpdateBoardEvent [blocks=" + Arrays.toString(blocks)
				+ ", clientID=" + clientID + ", rotation=" + rotation
				+ ", tetromino=" + tetromino + ", x=" + x + ", y=" + y + "]";
	}
	
	// Parcelable stuff below
	
	public static final Parcelable.Creator<UpdateBoardEvent> CREATOR = new Creator<UpdateBoardEvent>() {
		
		public UpdateBoardEvent[] newArray(int size) {
			return new UpdateBoardEvent[size] ;
		}
		
		public UpdateBoardEvent createFromParcel(Parcel source) {
			final String clientID = source.readString();
			final int x = source.readInt();
			final int y = source.readInt();
			final int rotation = source.readInt();
			final char tetromino = source.readString().charAt(0);
			final int[] blocks = new int[WKC.BOARD_WIDTH*WKC.BOARD_HEIGHT] ;
			source.readIntArray(blocks) ;
			
			return new UpdateBoardEvent(clientID, x, y, rotation, tetromino, blocks) ;
		}
	};

	public int describeContents() {
		return 0;
	}

	public void writeToParcel(Parcel dest, int flags) {
		dest.writeString(clientID) ;
		dest.writeInt(x) ;
		dest.writeInt(y);
		dest.writeInt(rotation) ;
		dest.writeString("" + tetromino) ;
		dest.writeIntArray(blocks) ;
	}
}
